package com.df.bpm.utils.businessViewer;

import com.df.bpm.entity.BusinessInformation;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务路径表
 */
public class BusinessPathTable extends JScrollPane {
    JTable dataTable=new JTable();
    List<BusinessInformation> pathBusiness;

    public BusinessPathTable(){
        pathBusiness=new ArrayList<>();
        BusinessInformation topBusiness=new BusinessInformation();
        topBusiness.setBusinessInformationIndex(-1);
        topBusiness.setBusinessDesc("顶级流程");
        pathBusiness.add(topBusiness);
    }

    public void addPath(BusinessInformation newPath){
        pathBusiness.add(newPath);
    }

    /**
     * 刷新
     */
    public void fresh(){
        String[] title={"ID","业务描述"};
        Object[][] info = new Object[pathBusiness.size()][title.length];
        for(int i=0;i<pathBusiness.size();i++){
            info[i][0]=pathBusiness.get(i).getBusinessInformationIndex();
            info[i][1]=pathBusiness.get(i).getBusinessDesc();
        }
        DefaultTableModel model=new DefaultTableModel(info,title){
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;//不允许编辑，留作触发双击或拖拽
            }
        };
        dataTable=new JTable(model);
        setTitleFont();
        setTableFont();
        Main.hideCol(dataTable,0);
        this.getViewport().add(dataTable);
    }

    /**
     * 退回路径节点
     * @param row 行号
     */
    public void toPath(int row) {
        pathBusiness=pathBusiness.subList(0, row+1);
    }

    /**
     * 调整标题字体
     */
    private void setTitleFont(){
        dataTable.getTableHeader().setFont(new Font("宋体", Font.BOLD, (int) (MainPanel.unit*1.1)));
    }

    /**
     * 调整table内容字体
     */
    private void setTableFont(){
        dataTable.setFont(new Font("宋体", Font.PLAIN, (int) (MainPanel.unit)));
        dataTable.setRowHeight((int) (MainPanel.unit+1));
    }


}
